package com.kiln.juxingqs.fragment.market

import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.ImageView
import client.global.commlib.base.BaseFragment
import client.global.commlib.utils.gson
import client.global.commlib.widget.EmptyLayout
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.github.mikephil.charting.stockChart.dataManage.TimeDataManage
import com.kiln.juxingqs.R
import com.kiln.juxingqs.databinding.FragmentFiveDayBinding
import com.kiln.juxingqs.ui.vm.KLineModel
import com.kiln.juxingqs.utils.DateUtils
import org.json.JSONException
import org.json.JSONObject

/**
 * 分时页
 */
class ChartFiveDayFragment : BaseFragment<FragmentFiveDayBinding, KLineModel>() {
    private var land = false //是否横屏
    private val kTimeData = TimeDataManage()
    var goodsId: String? = null

    override fun getViewId(): Int {
        return R.layout.fragment_five_day
    }

    override fun initViewModel(): KLineModel {
        return createViewModel()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        land = requireArguments().getBoolean("landscape")
        goodsId = requireArguments().getString("goodsId")

    }

    override fun initWidget() {
        super.initWidget()
        binding.chart!!.initChart(land) //初始化
        viewModel.kType.value = 1 //5日
        binding.emptyLayout.setOnClickListener {
            viewModel.isOpenTime.value = DateUtils.isBelong(viewModel.begin_time, viewModel.end_time)
            viewModel.hole5Day(goodsId!!, 1)
        }
    }

    override fun initData() {
        super.initData()
        viewModel.call5d.observe(this, {
            it?.let {
                if (it.isSuccess) {
//                    var m5 = GsonUtils.toJson(it.data!!)
                    try {
                        val m5Json =
                            JSONObject(gson.toJson(it.data)) //m5，测试数据 ChartData.FiveTIMEDATA
                        kTimeData.parseTimeData(m5Json, "000001.IDX.SH", 0.0)
                        binding.chart.setDataToChart(kTimeData, viewModel.isOpenTime.value)
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }

                    viewModel.call5d.value = null
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    if (binding.chart.getmData() == null) {
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                    }
                }

                handler.postDelayed(runnable, 5000) //每5秒执行一次runnable. -第一步
            }
        })

        /*
        //        ivLand.setOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View v) {
        //                Intent intent = new Intent(getActivity(), StockDetailLandActivity.class);
        //                getActivity().startActivity(intent);
        //            }
        //        });
        //        if (!land) {
        //            ivLand!!.visibility = View.VISIBLE
        //        } else {
        //            ivLand!!.visibility = View.GONE
        //        }
                */

        /*//非横屏页单击转横屏页
        if (!land) {
            binding.chart.getGestureListenerLine().setCoupleClick(CoupleChartGestureListener.CoupleClick {
                startActivity(Intent(context, StockDetailLandActivity::class.java).apply {
                    putExtra("goodsId", "${goodsId}")
                })

            })
            binding.chart.getGestureListenerBar().setCoupleClick(CoupleChartGestureListener.CoupleClick {
                startActivity(Intent(context, StockDetailLandActivity::class.java).apply {
                    putExtra("goodsId", "${goodsId}")
                })
            })
        }*/

    }

    companion object {

        fun newInstance(land: Boolean, goodsId: String): ChartFiveDayFragment? {
            val fragment = ChartFiveDayFragment()
            val bundle = Bundle()
            bundle.putBoolean("landscape", land)
            bundle.putString("goodsId", goodsId)
            fragment.arguments = bundle
            return fragment
        }
    }

    val handler = Handler()

    val runnable: Runnable = object : Runnable {
        override fun run() {
//            //要做的事情
            if (DateUtils.isBelong(viewModel.begin_time, viewModel.end_time)) {
                //实时刷新
                viewModel.kType.value = 1
                viewModel.isOpenTime.value = true
                viewModel.hole5Day(goodsId!!, 1)
                LogUtils.i("在${viewModel.begin_time} - ${viewModel.end_time}，时间范围内")
            } else {
//                LogUtils.i("非开盘时间范围内")
                handler.postDelayed(this, 5000) //每5秒执行一次runnable. -第一步
                viewModel.isOpenTime.value = false
            }
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.isOpenTime.value = DateUtils.isBelong(viewModel.begin_time, viewModel.end_time)
        viewModel.hole5Day(goodsId!!, 1)
    }

    override fun onPause() {
        super.onPause()
        handler.removeCallbacks(runnable);
        handler.removeCallbacksAndMessages(null)
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacks(runnable);
        handler.removeCallbacksAndMessages(null)
    }
}